<?php

class User_Model_Estat extends Zend_Db_Table_Abstract
{
    protected $_name = 'estat';
    protected $_primary = 'mail';
   
    
    public function get_dades($mail, &$nom, &$posX, &$posY){
        $select = $this->_db->select()
                    ->from($this->_name)
                    ->where('mail = "'.$mail.'"');
        $result = $select->query()->fetchAll();
        
        $select = $this->_db->select()
                    ->from('taxistes')
                    ->where('mail = "'.$mail.'"');
        $result2 = $select->query()->fetchAll();
        
        if(sizeof($result) == 0 || sizeof($result2) == 0)    return false;
        $nom = $result2[0]['nom'];
        $posX = $result[0]['posx'];
        $posY = $result[0]['posy'];
        return true;
    }
    
    public function peticio_diferida($mail_taxi){
        $data = array('diferida' => 1);
        $this->update($data, 'mail = "'.$mail_taxi.'"');
    }
    
    public function peticio($mail_taxi, $id_sol){
        
        $data = array('id_solicitud' => $id_sol);
        $this->update($data, 'mail = "'.$mail_taxi.'"');
    }
    
    public function pregunta_al_mes_proper($posX, $posY){
        
        $select = $this->_db->select()
                    ->from($this->_name)
                    ->where('logged = 1 and id_solicitud = -1');
        
        $result = $select->query()->fetchAll();
        
        $d = -1;
        $j = -1;
        for($i = 0; $i < sizeof($result); $i++){
            $d_aux = $this->dist($posX, $posY, $result[$i]['posx'], $result[$i]['posy']);
            if($d == -1 || $d_aux < $d){
                $d = $d_aux;
                $j = $result[$i]['mail'];
            }
        }
        
        return $j;
    }

  
    private function dist($aX, $aY, $bX, $bY){
        return sqrt((($aX-$bX)*($aX-$bX))+(($aY-$bY)*($aY-$bY)));
    }

}
?>
